package net.oschina.kjtang.qywx.api.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import net.oschina.kjtang.qywx.api.entity.Menu;
import net.oschina.kjtang.qywx.api.service.WxMenuService;
import net.oschina.kjtang.qywx.common.component.httpclient.HttpClientComponent;
import net.oschina.kjtang.qywx.common.component.httpclient.HttpResult;
import net.oschina.kjtang.qywx.common.constants.RCodeConstants;
import net.oschina.kjtang.qywx.common.constants.WxAPIConstants;
import net.oschina.kjtang.qywx.common.entity.Entity;
import net.oschina.kjtang.qywx.common.exception.RCodeException;
import net.oschina.kjtang.qywx.common.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by kjtang on 2017/12/1.
 */
@Service
public class WxMenuServiceImpl extends BaseServiceImpl implements WxMenuService {

    @Autowired
    private HttpClientComponent httpClientComponent;

    @Override
    public void addMenu(Menu menu, String accessToken, String agentid) throws RCodeException {
        try{
            String jsonMenu = JSON.toJSONString(menu);
            System.out.println(jsonMenu);
            HttpResult httpResult = httpClientComponent.doPostJson(WxAPIConstants.MENU_CREATE_URL.replace("ACCESS_TOKEN",accessToken).replace("AGENTID",agentid),jsonMenu);
            if(httpResult.getStatus()==200){
                JSONObject jsonObject = JSON.parseObject(httpResult.getData());
                if(jsonObject!=null){
                    int errcode = jsonObject.getIntValue("errcode");
                    String errmsg = jsonObject.getString("errmsg");
                    if(errcode!=0){
                        logger.info("[DELETEMENU]", "deleteMenu response:{}", new Object[]{jsonObject.toJSONString()});
                        throw  new RCodeException(errcode,new RuntimeException(errmsg));
                    }
                }else{
                    throw  new RCodeException(2323,new RuntimeException("新增菜单失败"));
                }
            }
        }catch (Exception e){
            throw new RCodeException(3434,e);
        }

    }

    @Override
    public void deleteMenu(String accessToken, String agentid) throws RCodeException {
        try {
            HttpResult httpResult = httpClientComponent.doPost(WxAPIConstants.MENU_DELETE_URL.replace("ACCESS_TOKEN", accessToken).replace("AGENTID", agentid));
            if(httpResult.getStatus()==200){
                JSONObject jsonObject = JSON.parseObject(httpResult.getData());
                if(jsonObject!=null){
                    int errcode = jsonObject.getIntValue("errcode");
                    String errmsg = jsonObject.getString("errmsg");
                    if(errcode!=0){
                        throw  new RCodeException(RCodeConstants.FAIL,new RuntimeException(errmsg));
                    }
                }else{
                    throw  new RCodeException(RCodeConstants.FAIL,new RuntimeException("删除菜单失败"));
                }
            }
        }catch (Exception e){
            throw  new RCodeException(RCodeConstants.FAIL,e);
        }
    }

    @Override
    public Menu getMenu(String accessToken, String agentid) throws RCodeException {
        Menu menu=null;
        try {
            HttpResult httpResult = httpClientComponent.doPost(WxAPIConstants.MENU_GET_URL.replace("ACCESS_TOKEN", accessToken).replace("AGENTID", agentid));
            if(httpResult.getStatus()==200){
                JSONObject jsonObject = JSONObject.parseObject(httpResult.getData());
                if(jsonObject!=null){
                    int errcode = jsonObject.getIntValue("errcode");
                    String errmsg = jsonObject.getString("errmsg");
                    if(errcode!=0){
                        throw  new RCodeException(RCodeConstants.FAIL,new RuntimeException(errmsg));
                    }else{
                        menu = JSON.toJavaObject(jsonObject,Menu.class);
                    }
                }
            }
        }catch (Exception e){
            throw  new RCodeException(RCodeConstants.FAIL,e);
        }
        return menu;
    }
}
